May be Incompatible Cast (MIC)

Description:

MIC detects situations when a method may return values of several incompatible types and its result is cast to one of them without a type check. Such situations may cause a class cast exception at runtime.

Incorrect:

class Record { 
    int id;

    public object getId() { 
        if (id == -1) { 
            return "null";
        }
        return id;
    }

    public int GetHashCode() { 
        return (int)getId();
    }
}

Correct:

class Record { 
    int id;

    public object getId() { 
        if (id == -1) { 
            return "null";
        }
        return id;
    }

    public int GetHashCode() { 
        object o = getId();
        return o is int ? (int)o : 0;
    }
}